import signal


# 自定义输入超时异常
class InputTimeoutError(Exception):
    pass


# 定义信号到达后的处理函数
def interrupted(signum, frame):
    raise InputTimeoutError


# 定义超时退出的输入函数
def input_with_timeout(prompt, timeout):
    signal.signal(signal.SIGALRM, interrupted)
    signal.alarm(timeout)
    try:
        result = input(prompt)
    except InputTimeoutError:
        # print('\n超时')
        result = ''
    signal.alarm(0)  # 读到输入的话重置信号
    return result


# 定义猜谜函数
def caimi():
    score = 0
    # 请自行补充谜面和谜底字典
    miyu = {"黯。（打一成语）": "有声有色", "泵。（打一成语）": "水落石出","乖。（打一成语）":"乘人不备","龙。（打一成语）":"充耳不闻"}
    print("猜谜语游戏开始（想中途退出，请按q键）：")
    for mimian, midi in miyu.items():
        print(mimian)
        guess = input_with_timeout("请在10秒内输入谜底：", 10)
        guess = guess.strip()
        if guess == midi:
            score += 1
            print("猜对啦!")
        elif guess == "q":
            break
        else:
            print("猜错啦!")

    print("您的最终得分是：", score)


if __name__ == "__main__":
    caimi()

